<?php

namespace App\Http\Controllers\Admin;

use App\Models\User;
use App\Services\Admin\SystemSet;
use App\Services\Api\UserRankExpServices;
use Illuminate\Support\Facades\DB;
use App\Http\Controllers\Controller;

class SystemController extends Controller
{

    public function index()
    {

        return view('admin.system.index');
    }

    public function do(UserRankExpServices $userRankExpServices)
    {
        $class_base_info = DB::table('class_base_info')->where('teacher_id', '!=', '0')->select('id', 'teacher_id')->get()->toArray();

        $class_base_info_teacher_sql = DB::table('class_base_info_teacher')->get();

        if ($class_base_info_teacher_sql->isEmpty()) {

            DB::table('class_base_info_teacher')->truncate();

            $class_base_info_teacher = [];

            foreach ($class_base_info as $v) {
                $input = [];
                $input['class_base_info_id'] = $v->id;
                $input['teacher_id'] = $v->teacher_id;
                $input['is_common'] = 1;

                $class_base_info_teacher[] = $input;
            }

            DB::table('class_base_info_teacher')->insert($class_base_info_teacher);

        }

        /**积分更改等级**/

        $user_ids = DB::table('user')->where('user_rank_id', '0')->pluck('id')->toArray();

        foreach ($user_ids as $v) {
            $user_rank = $userRankExpServices->get_user_rank($v);
            $userObj = User::where('id', $v)->select('id')->first();

            $userObj->user_rank_id = $user_rank['id'];
            $userObj->user_rank_level = $user_rank['level'];
            $userObj->save();
        }

        return self::success([]);
    }

    public function system_message()
    {
        if(request()->ajax()){

            SystemSet::system_message_do();

            return self::success([]);

        }

        $result = [
            'result' => SystemSet::system_message()
        ];

        return view('admin.system.message',$result);
    }
}
